package com.example.ccj.b.c.dialog;

import android.content.Context;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.ccj.R;
import com.example.ccj.b.c.custom.adapter.MultiChoiceAdapter;
import java.util.ArrayList;
import java.util.List;

/**
 * 选择选项
 */
public class MultiChoiceDialog<T> extends DialogBase {
    List<T> t;
    List<T> selected;
    MultiChoiceListener<T> multiChoiceListener;

    public MultiChoiceDialog(@NonNull Context context, List<T> t, List<T> selected, MultiChoiceListener<T> multiChoiceListener) {
        super(context);
        this.t = t;
        this.selected = new ArrayList<>(selected);
        this.multiChoiceListener = multiChoiceListener;
    }

    @Override
    public void onInit() {
        isCanClose(true);
        setLayout(R.layout.dialog_multi_option);
        setAlpha(1f);//透明度
        isCanClose(true);//点击允许关闭
        isShowBackground(false);//不显示背景
        setShowHeightRatio(true);//按比例显示
        setHeightRatio(0.5);//高度显示比例
        showInBottom();//底部弹出
        setFull(true);//是否铺满
        goneTitleLeftImage();


        RecyclerView recyclerView = findViewById(R.id.dialog_multi_option_rec);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(layoutManager);


        recyclerView.setAdapter(new MultiChoiceAdapter<>(t, selected,  new MultiChoiceListener<T>() {
            @Override
            public String formatItem(@NonNull T item) {
                if (multiChoiceListener != null) return multiChoiceListener.formatItem(item);
                return item.toString();
            }

            @Override
            public void onSelect(List<T> tList) {

            }

            @Override
            public void onSelect(T user, int position) {
                if (selected == null) selected = new ArrayList<>();
                selected.add(user);
            }

            @Override
            public void onRemove(T user, int position) {
                selected.remove(user);
            }
        }));


        //取消按钮
        this.setTitleLeftText("取消").setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        //确定按钮
        this.setTitleRightText("ok").setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (multiChoiceListener != null) multiChoiceListener.onSelect(selected);
                dismiss();
            }
        });

    }



}
